package cn.xo68.boot.auth.server.web.api;

import java.util.List;

import cn.xo68.boot.auth.core.common.AuthConstants;
import cn.xo68.boot.auth.core.domain.Oauth2Principal;
import cn.xo68.boot.auth.server.domain.OauthRole;
import cn.xo68.boot.auth.server.domain.OauthRoleResource;
import cn.xo68.boot.auth.server.domain.dto.OauthUserRoleResourceDto;
import cn.xo68.boot.auth.server.service.OauthRoleResourceService;
import cn.xo68.core.date.DateTime;
import cn.xo68.core.util.StringTools;
import cn.xo68.core.util.UUIDUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.xo68.boot.auth.server.service.OauthRoleService;



/** 
 * OauthRole Service
 * @author wuyy(16349023@qq.com)
 * @date 2018年09月05日 11:00:41
 *
 */
@RequestMapping("/api/OauthRole")
@RestController
public class OauthRoleController{
    @Autowired
    private OauthRoleService oauthRoleService;

    @Autowired
    private OauthRoleResourceService oauthRoleResourceService;

    @GetMapping("/resourcs")
    public List<String> listRoleResourcs(@RequestParam(name="roleId")String roleId,@RequestParam(name="clientId") String clientId) {
        return  oauthRoleResourceService.listRoleResourcs(roleId,clientId);
    }

    @PostMapping("/saveResource")
    public String saveResource(@RequestBody OauthUserRoleResourceDto oauthUserRoleResourceDto,Subject subject, Oauth2Principal oauth2Principal){
        List<OauthRoleResource> resources = oauthUserRoleResourceDto.buildRoleResources(oauth2Principal.getUserName(), oauth2Principal.getUserId());
        oauthRoleResourceService.saveResources(oauthUserRoleResourceDto.getClientId(), oauthUserRoleResourceDto.getRoleId(), resources);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
    }

	@GetMapping("/find")
    public OauthRole find(@RequestParam(name="id") String id) {
        return oauthRoleService.get(id);
    }

    @GetMapping("/all")
    public List<OauthRole> all(){
        return oauthRoleService.list();
    }

    @GetMapping("/list")
    public PageInfo<OauthRole> list(@RequestParam(name="pageIndex",defaultValue="1") int pageIndex, @RequestParam(name="pageSize",defaultValue="10") int pageSize){
        return oauthRoleService.list(pageIndex, pageSize);
    }
	
	@PostMapping("/add")
    public String add(@RequestBody OauthRole entity,Subject subject, Oauth2Principal oauth2Principal) {
	    if(StringTools.isEmpty(entity.getRoleId())){
	        entity.setRoleId(UUIDUtils.createUUId());
        }

        entity.setOperatorId(oauth2Principal.getUserId());
        entity.setOperatorName(oauth2Principal.getUserName());
        entity.setCreateTime(DateTime.Now().getDate());
        entity.setUpdateTime(DateTime.Now().getDate());
        oauthRoleService.insert(entity);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
    }
	
	@PostMapping("/delete")
	public String delete(@RequestParam(name="id") String id) {
	    oauthRoleService.delete(id);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
	}
	
	@PostMapping("/update")
	public String update(@RequestBody OauthRole entity,Subject subject, Oauth2Principal oauth2Principal) {
	    entity.setOperatorId(oauth2Principal.getUserId());
	    entity.setOperatorName(oauth2Principal.getUserName());
	    entity.setUpdateTime(DateTime.Now().getDate());
	    oauthRoleService.update(entity);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
	}
}